package com.lanou.sfu.songforu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.SearchSingSongAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.datas.SearchSingleSong;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by quyan on 15/10/28.
 */
public class SearchingSingleSongFragment extends BaseFragment {
    private TextView songNum;
    private RelativeLayout singleSongLayout;
    private ExpandableListView singleSongList;
    private String songUrl;
    private List<SearchSingleSong.DataEntity> datas;
    private SearchSingSongAdapter adapter;
    private VolleySingleton volleySingleton;

    //静态方法 用来接收从容器传过来的数据
    public static SearchingSingleSongFragment passData(Context mContext, String str) {
        SearchingSingleSongFragment fragment = new SearchingSingleSongFragment();
        Bundle bundle = new Bundle();
        bundle.putString("str", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_singlesong, null);
        songNum = (TextView) view.findViewById(R.id.song_count);
        singleSongLayout = (RelativeLayout) view.findViewById(R.id.singlesong_layout);
        singleSongList = (ExpandableListView) view.findViewById(R.id.singlesong_list);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        volleySingleton = VolleySingleton.getInstance(getActivity());
        Bundle bundle = getArguments();
        String url = bundle.getString("str");
        songUrl = Url.SEARCHING_SINGLE_SONG1 +
                Url.SEARCHING_SINGLE_SONG2 +
                Url.SEARCHING_SINGLE_SONG3 +
                Url.SEARCHING_SINGLE_SONG4 +
                Url.SEARCHING_SINGLE_SONG5 +
                Url.SEARCHING_SINGLE_SONG6 +
                Url.SEARCHING_SINGLE_SONG7 +
                Url.SEARCHING_SINGLE_SONG8 +
                url + Url.SEARCHING_SINGLE_SONG9;
        initData();
    }

    //解析数据
    public void initData() {
        StringRequest request = new StringRequest(songUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchSingleSong singleSong = gson.fromJson(response, SearchSingleSong.class);
                songNum.setText(singleSong.getData().size() + "");//歌曲数量
                singleSongLayout.setVisibility(View.VISIBLE);
                //传数据给适配器
                datas = singleSong.getData();
                adapter = new SearchSingSongAdapter(getActivity(), datas);
                singleSongList.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);

    }
}
